package com.thkj.common.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.thkj.common.util.RmLoginUserInfo;
import com.thkj.common.util.RmUtil;
import com.thkj.common.util.Util;

public class LoginInterceptor extends HandlerInterceptorAdapter {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		// 判断来自手机端还是PC端的访问
		String requestHeader = request.getHeader("user-agent");
		if (Util.isMobileDevice(requestHeader)) {
			request.setAttribute("isMobile", true);
		} else {
			request.setAttribute("isMobile", false);
		}
		RmLoginUserInfo loginUserInfo = RmUtil.getLoginUserFromCookie(request);
		// 如果cookie没有登录信息则跳转到首页，有登录信息则转换为对象存入到request方便取用
		if (loginUserInfo == null) {
			RmUtil.noLogin(request, response);
			return false;
		} else {
			RmUtil.setLoginUserInfo(request, loginUserInfo);
			return true;
		}
	}

}
